package com.huahang.fashionpress.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huahang.fashionpress.enums.ExceptionEnum;
import com.huahang.fashionpress.exception.ResultException;
import com.huahang.fashionpress.form.FormAdmin;
import com.huahang.fashionpress.mapper.AdminMapper;
import com.huahang.fashionpress.model.Admin;
import com.huahang.fashionpress.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: 阿俊哥 李梦阳
 * @Date: 2019/8/23 13:56
 * @Version 1.0
 */
@Service
@Slf4j
public class AdminServiceImpl implements AdminService {

    @Resource
    AdminMapper adminMapper;
    @Override
    public List<Admin> slelectAdminList() {

       return adminMapper.selectAdminList();
    }

    @Override
    public Admin selectAdminById(int adminId) {
        if("0".equals(String.valueOf(adminId))||String.valueOf(adminId)==null){
            log.info("【后台用户管理】管理员编号数据出错");
            throw  new ResultException(ExceptionEnum.DATA_ERROR);
        }
        Admin admin  = adminMapper.selectById(adminId);
        if(admin==null){
            log.info("【后台用户管理】编号："+adminId+"管理员不存在");
            throw new ResultException(ExceptionEnum.ADMIN_NOT_EXIT);
        }
        return  admin;
    }

    @Override
    public Admin selectAdminByName(String adminName) {
        if(adminName==null||adminName==""){
            log.info("【后台用户管理】管理员编号数据出错");
            throw  new ResultException(ExceptionEnum.DATA_ERROR);
        }
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<Admin>();
        queryWrapper.eq("admin_username", adminName);
        List<Admin> admins =  adminMapper.selectList(queryWrapper);
        if(admins.size()==0){
            log.info("【后台用户管理】未找到管理员");
            return null;
        }
        return admins.get(0);
    }


    @Override
    public boolean updateAdmin(Admin admin) {

        int result = adminMapper.updateById(admin);
        if(result>0)
            return true;
        return false;
    }

    @Override
    public boolean deleteAdmin(int adminId) {
        if("0".equals(String.valueOf(adminId))||String.valueOf(adminId)==null){
            log.info("【后台用户管理】管理员编号数据出错");
            throw  new ResultException(ExceptionEnum.DATA_ERROR);
        }
        int result = adminMapper.deleteById(adminId);
        if(result>0)
            return true;
        return false;
    }

    @Override
    public boolean addAdmin(Admin admin) {
        int result = adminMapper.insert(admin);
        if(result>0)
            return true;
        return false;
    }


    @Override
    public List<FormAdmin> SelectFAdminlist() {
        return adminMapper.SelectFAdminlist();
    }

    @Override
    public FormAdmin SelectFAdminByadminId(int adminId) {
        if("0".equals(String.valueOf(adminId))||String.valueOf(adminId)==null){
            log.info("【后台用户管理】管理员编号数据出错");
            throw  new ResultException(ExceptionEnum.DATA_ERROR);
        }
        FormAdmin admin =  adminMapper.SelectFAdminByadminId(adminId);
        if(admin==null){
            log.info("【后台用户管理】编号："+adminId+"管理员不存在");
            throw new ResultException(ExceptionEnum.ADMIN_NOT_EXIT);
        }
        return admin;
    }

    @Override
    public List<FormAdmin> SelectFAdminBynickName(String nickName) {
        if(nickName==null||nickName==""){
            log.info("【后台用户管理】管理员角色数据出错");
            throw  new ResultException(ExceptionEnum.DATA_ERROR);
        }
        List<FormAdmin> formAdminList  = adminMapper.SelectFAdminBynickName(nickName);
        if(formAdminList==null){
            log.info("【后台用户管理】编号："+nickName+"管理员不存在");
            throw new ResultException(ExceptionEnum.ADMIN_NOT_EXIT);
        }
        return formAdminList;
    }

    @Override
    public void updateAdminStatus(Admin admin) {
        if(admin.getAdminStatus()==0){
            admin.setAdminStatus(1);
        }else{
            admin.setAdminStatus(0);
        }
        adminMapper.updateById(admin);
    }



}
